/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sales.seesion.stateless;

import company.entity.Company;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import sales.entity.LineItem;
import sales.entity.Product;
import sales.entity.SalesInquiry;
import sales.util.exception.SalesException;

/**
 *
 * @author frankxgz
 */
@Stateless
@LocalBean
public class SalesInquirySessionBean {

    @PersistenceContext
    private EntityManager em;
    private PriceManageSessionBean priceManage = new PriceManageSessionBean();
    private Company company;
    
    public SalesInquiry addSalesInquiry(Long companyId, List productIds, List quantities)throws SalesException{
        SalesInquiry inquiry = new SalesInquiry();
        company = em.find(Company.class, companyId);        
        inquiry.setCompany(company);
        em.persist(inquiry);
        
        List<LineItem> lineItems = createLineItems(createProductList(productIds),quantities);
        inquiry.setInquiryLineItems(lineItems);
        
        return inquiry;
    }
    
    public SalesInquiry saveSalesInquiry(Long inquiryId,List productIds, List quantities)throws SalesException{
        SalesInquiry inquiry = em.find(SalesInquiry.class,inquiryId);
        company = inquiry.getCompany();
        List<LineItem> lineItems = createLineItems(createProductList(productIds),quantities);
        inquiry.setInquiryLineItems(lineItems);
        em.merge(inquiry);
        return inquiry;
    }
    
    public void deleteInquiryLineItems(Long inquiryId){
        SalesInquiry inquiry = em.find(SalesInquiry.class, inquiryId);
        List <LineItem> lineItems = inquiry.getInquiryLineItems();
        for (Object o: lineItems){
            LineItem li = (LineItem)o;
            em.remove(li);
        }
        inquiry.setInquiryLineItems(null);
        em.flush();
    }
    
    public void deleteSalesInquiry(Long inquiryId){
        SalesInquiry inquiry = em.find(SalesInquiry.class,inquiryId);
        List <LineItem> lineItems = inquiry.getInquiryLineItems();
        for (Object o: lineItems){
            LineItem li = (LineItem)o;
            em.remove(li);
        }
        em.remove(inquiry);
    }
    
    public List<SalesInquiry> getAllSalesInquiry(){
        Query query = em.createQuery("SELECT s1 FROM SalesInquiry s1");
        return query.getResultList();
    }
    
    public Company getCompany(Long id){
        Company company = em.find(Company.class, id);
        return company;
    }
    
    public Company getCompanyByName(String companyName){
         
        Query query = em.createQuery("SELECT c FROM Company c WHERE c.name = :inCompanyName");
        query.setParameter("inCompanyName", companyName);
        Company company = null;
        try{
            company = (Company)query.getSingleResult();
        }
        catch(NoResultException ex){
            ex.printStackTrace();
        }
        return company;
     }
    
    public Product getProduct(Long id){
        Product product = em.find(Product.class, id);
        return product;
    }
    
    public List createProductList(List productIds){
        List<Product> products = new ArrayList<Product>();
        Product product;
        for (Object o: productIds){
            Long productId = (Long)o;
            product = getProduct(productId);
            products.add(product);
        }
        return products;
    }
    
    public List<LineItem> createLineItems(List products, List quantities)throws SalesException{
        List<LineItem> lineItems = new ArrayList<LineItem>();
        
        int length = products.size();
        for (int i=0; i<length; i++){
            LineItem lineItem = new LineItem();
            lineItem.setProduct((Product)products.get(i));
            lineItem.setQuantity(((Integer)quantities.get(i)).intValue());
            double price = priceManage.calculatePirce(company.getCompanyType(),(Product)products.get(i),((Integer)quantities.get(i)).intValue());
            lineItem.setPrice(price);
            em.persist(lineItem);
            lineItems.add(lineItem);
        }
        return lineItems;
    }
    
    public SalesInquiry getInquiryById(Long id){
        try{
            SalesInquiry iq = em.find(SalesInquiry.class,id);
            return iq;
        }catch (NoResultException ex) {
            return null;
        }
    }
    
}
